package org.example.infrastructure.adapter.repository;

import org.example.domain.strategy.model.entity.StrategyAwardEntity;
import org.example.domain.strategy.repository.IStrategyRepository;
import org.example.infrastructure.dao.IStrategyAwardDao;
import org.example.infrastructure.dao.IStrategyDao;
import org.example.infrastructure.dao.po.StrategyAward;
import org.example.infrastructure.redis.RedissonService;
import org.example.types.common.Constants;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@Repository
public class StrategyRepository implements IStrategyRepository {
    @Resource
    private RedissonService redissonService;
    @Resource
    private IStrategyAwardDao strategyAwardDao;
    @Resource
    private IStrategyDao strategyDao;
    @Override
    public boolean queryIfExistStrategyAwardSearchRateMap(Long StrategyId) {
        return redissonService.isExists(Constants.STRATEGY_RATE_RANGE_KEY + StrategyId) && redissonService.isExists(Constants.STRATEGY_RATE_MAP_KEY + StrategyId);
    }

    @Override
    public List<StrategyAwardEntity> queryStrategyAwardList(Long strategyId) {
        List<StrategyAward> strategyAwardsList =  strategyAwardDao.queryStrategyAwardList(strategyId);
        List<StrategyAwardEntity> res = new ArrayList<>();
        for(StrategyAward tmp : strategyAwardsList) {
            StrategyAwardEntity strategyAwardEntity = StrategyAwardEntity.builder()
                    .awardId(tmp.getAwardId())
                    .awardRate(tmp.getAwardRate())
                    .sort(tmp.getSort())
                    .build();
            res.add(strategyAwardEntity);
        }
        return res;
    }

    @Override
    @Transactional
    public void storeStrategyRateMapAndRange(String strategyId, Map<Integer, Long> map, Integer range) {
        Map<Integer, Long> rMap = redissonService.getMap(Constants.STRATEGY_RATE_MAP_KEY + strategyId);
        rMap.putAll(map);
        redissonService.setValue(Constants.STRATEGY_RATE_RANGE_KEY + strategyId, range);
    }

    @Override
    public Integer getRateRange(Long strategyId) {
        return redissonService.getValue(Constants.STRATEGY_RATE_RANGE_KEY + strategyId);
    }

    @Override
    public Integer getRateRange(String key) {
        return redissonService.getValue(Constants.STRATEGY_RATE_RANGE_KEY + key);
    }

    @Override
    public Long getStrategyAwardIdFromMap(Long strategy, Integer randomKey) {
        Map<Integer, Long> map = redissonService.getMap(Constants.STRATEGY_RATE_MAP_KEY + strategy);
        return map.get(randomKey);
    }

    @Override
    public Long getStrategyAwardIdFromMap(String key, Integer randomKey) {
        Map<Integer, Long> map = redissonService.getMap(Constants.STRATEGY_RATE_MAP_KEY + key);
        return map.get(randomKey);
    }

    @Override
    public List<String> queryRuleModelsByStrategyId(Long strategyId) {
        String ruleModels = strategyDao.queryRuleModels(strategyId);
        if (ruleModels != null) {
            return Arrays.stream(ruleModels.split(Constants.SPLIT)).toList();
        }
        return null;
    }
}
